package com.soilflow.info.excelListenner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soilflow.common.core.domain.entity.SysDept;
import com.soilflow.common.domain.LandClassify;
import com.soilflow.common.domain.LandInfo;
import com.soilflow.common.excle.im.InfoExcel;
import com.soilflow.info.service.LandClassifyService;
import com.soilflow.info.service.LandInfoService;
import com.soilflow.system.service.ISysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class landInfoListenner extends AnalysisEventListener<InfoExcel> {

    public LandInfoService landInfoService;

    public ISysDeptService deptService;

    public LandClassifyService soilflowClassifyService;

    public landInfoListenner() {}

    public landInfoListenner(LandInfoService landInfoService,ISysDeptService deptService,LandClassifyService soilflowClassifyService){
        this.landInfoService=landInfoService;
        this.deptService=deptService;
        this.soilflowClassifyService=soilflowClassifyService;
    }



    @Override
    public void invoke(InfoExcel landInfoExcel, AnalysisContext analysisContext) {
        if (landInfoExcel!=null) {
            String deptName = landInfoExcel.getDeptName();
            Long deptId = deptService.selectDeptId(deptName);
            System.out.println("deptId"+deptId);
            LandInfo landInfo=new LandInfo();
            BeanUtils.copyProperties(landInfoExcel,landInfo);
            //设置土地所在地区 execel表中为地区名字，数据表中为地区Id
            landInfo.setDeptId(deptId);
            //设置规划用途
            String plannedUse = landInfoExcel.getPlannedUse();
            if (plannedUse.equals("加油站")) {
                landInfo.setPlannedUse("0");
            } else if (plannedUse.equals("电影院")) {
                landInfo.setPlannedUse("1");
            } else if (plannedUse.equals("商场")) {
                landInfo.setPlannedUse("2");
            }
            //设置土地形式
            String status = landInfoExcel.getStatus();
            if (status.equals("闲置中")) {
                landInfo.setStatus("0");
            } else if (status.equals("租赁中")) {
                landInfo.setStatus("1");
            }
            //设置土地分类
            String classifyName = landInfoExcel.getClassifyName();

            //如果填入的土地分类为商业用地或者农业用地时，是没有下级分类的
            if (classifyName.equals("商业用地") || classifyName.equals("农业用地")) {
                Long classIfyId = selectClassIfyId(classifyName);
                landInfo.setClassifyId(classIfyId);
            }else {
                //填入的为商场、茶园等,需要查出上级分类给landINnfo
                Long classIfyId = selectClassIfyId(classifyName);
                landInfo.setClassifyId(classIfyId);
                List<Long> landtypes = landtype(classIfyId);
                List<String> collect = landtypes.stream().map(x -> x + "").collect(Collectors.toList());
                landInfo.setLandTypeSet(JSONObject.toJSONString(collect));
            }
            saveData(landInfo);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
    private void saveData(LandInfo landInfo) {
        landInfo.setLandNo("TD17259844");
        landInfoService.save(landInfo);
    }

    public Long selectClassIfyId(String name){
        LambdaQueryWrapper<LandClassify> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LandClassify::getName, name);
        LandClassify one = soilflowClassifyService.getOne(wrapper);
        return one.getId();
    }
    public List<Long> landtype(Long classifyId){
        List<Long> list=new ArrayList<>();
        LambdaQueryWrapper<LandClassify> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LandClassify::getId, classifyId);
        LandClassify one = soilflowClassifyService.getOne(wrapper);
        Long parentId = one.getParentId();
        list.add(classifyId);
        list.add(parentId);
        return list;
    }
}
